/*****************************************************************************
*
*  PROJECT:		
*               
*  LICENSE:		
*
*  DEVELOPER:	
*
*****************************************************************************/

#include "EonCamera.h"

void Eon::Camera::setRotation( Eon::Vector3 newCameraRotation )
{
	cameraRotation = newCameraRotation;
}

void Eon::Camera::setPosition( Eon::Vector3 newCameraPosition )
{
	cameraPosition = newCameraPosition;
}

void Eon::Camera::init( Eon::Vector3 cameraPosition, Eon::Vector3 cameraRotation )
{
	// set the initial values for the position and rotation of the camera
	Eon::Camera::cameraPosition = cameraPosition;
	Eon::Camera::cameraRotation = cameraRotation;
}

void Eon::Camera::update( void )
{
	// Update the camera as you move it using the keybinds in the main program class
	glRotatef( cameraRotation.getX(), 1.0f, 0.0f, 0.0f );
	glRotatef( cameraRotation.getY(), 0.0f, 1.0f, 0.0f );
	glRotatef( cameraRotation.getZ(), 0.0f, 0.0f, 1.0f );
	glTranslatef( -cameraPosition.getX(), -cameraPosition.getY(), -cameraPosition.getZ() );
}

void Eon::Camera::moveForward( GLfloat cameraMovementSpeed )
{
	// Calculations to move camera forwards.
	GLfloat fYRotationInRadians = ( cameraRotation.getY() / 180 * M_PI );	// Converting to radians.
	// Get the current X position.
	GLfloat tempX = cameraPosition.getX();
	// Calculate the distance the camera moves and add it to the old X position.
	tempX += GLfloat( sin( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Get the current Z position.
	GLfloat tempZ = cameraPosition.getZ();
	// Calculate the distance the camera moves and add it to the old Z position.
	tempZ -= GLfloat( cos( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Set the new camera position vector.
	setPosition( Eon::Vector3( tempX, cameraPosition.getY(), tempZ ) );
}

void Eon::Camera::moveBackward( GLfloat cameraMovementSpeed )
{
	// Calculations to move camera backwards.
	GLfloat fYRotationInRadians = ( cameraRotation.getY() / 180 * M_PI );	// Converting to radians.
	// Get the current X position.
	GLfloat tempX = cameraPosition.getX();
	// Calculate the distance the camera moves and add it to the old X position.
	tempX -= GLfloat( sin( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Get the current Z position.
	GLfloat tempZ = cameraPosition.getZ();
	// Calculate the distance the camera moves and add it to the old Z position.
	tempZ += GLfloat( cos( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Set the new camera position vector.
	setPosition( Eon::Vector3( tempX, cameraPosition.getY(), tempZ ) );
}

void Eon::Camera::moveLeft( GLfloat cameraMovementSpeed )
{
	// Calculations to move camera forwards.
	// Subtract 90 degrees from the current camera Y rotation to get the movement along the "Z" axis to the left.
	GLfloat fYRotationInRadians = ( ( cameraRotation.getY() - 90 ) / 180 * M_PI );	// Converting to radians.
	// Get the current X position.
	GLfloat tempX = cameraPosition.getX();
	// Calculate the distance the camera moves and add it to the old X position.
	tempX += GLfloat( sin( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Get the current Z position.
	GLfloat tempZ = cameraPosition.getZ();
	// Calculate the distance the camera moves and add it to the old Z position.
	tempZ -= GLfloat( cos( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Set the new camera position vector.
	setPosition( Eon::Vector3( tempX, cameraPosition.getY(), tempZ ) );
}

void Eon::Camera::moveRight( GLfloat cameraMovementSpeed )
{
	// Calculations to move camera forwards.
	// Add 90 degrees to the current camera Y rotation to get the movement along the "Z" axis to the right.
	GLfloat fYRotationInRadians = ( ( cameraRotation.getY() + 90 ) / 180 * M_PI );	// Converting to radians.
	// Get the current X position.
	GLfloat tempX = cameraPosition.getX();
	// Calculate the distance the camera moves and add it to the old X position.
	tempX += GLfloat( sin( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Get the current Z position.
	GLfloat tempZ = cameraPosition.getZ();
	// Calculate the distance the camera moves and add it to the old Z position.
	tempZ -= GLfloat( cos( fYRotationInRadians ) ) * cameraMovementSpeed;
	// Set the new camera position vector.
	setPosition( Eon::Vector3( tempX, cameraPosition.getY(), tempZ ) );
}

void Eon::Camera::moveUp( GLfloat distanceToMoveOnYAxis )
{
	setPosition( Eon::Vector3( cameraPosition.getX(), cameraPosition.getY() + distanceToMoveOnYAxis, cameraPosition.getZ() ) );
}

void Eon::Camera::moveDown( GLfloat distanceToMoveOnYAxis )
{
	setPosition( Eon::Vector3( cameraPosition.getX(), cameraPosition.getY() - distanceToMoveOnYAxis, cameraPosition.getZ() ) );
}

void Eon::Camera::rotateLeft( GLfloat distanceRotatedOnYAxis )
{
	setRotation( Eon::Vector3( cameraRotation.getX(), cameraRotation.getY() - distanceRotatedOnYAxis, cameraRotation.getZ() ) );
}

void Eon::Camera::rotateRight( GLfloat distanceRotatedOnYAxis)
{
	setRotation( Eon::Vector3( cameraRotation.getX(), cameraRotation.getY() + distanceRotatedOnYAxis, cameraRotation.getZ() ) );
}

const Eon::Vector3& Eon::Camera::getPosition( void )
{
	return cameraPosition;
}

const Eon::Vector3& Eon::Camera::getRotation( void )
{
	return cameraRotation;
}